package org.example.controller;


import org.example.model.Record;
import org.example.model.User;
import org.example.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @PostMapping("/add/{awardId}")
    public Object add(@PathVariable Integer awardId,
                      @RequestBody List<Integer> ids){
        //传入的数据就是我们的awardId,memberId(多个)
        //插入数据：可以批量插入：List<Record>
        List<Record> records = new ArrayList<>();
        for(Integer memberId : ids){
            Record r = new Record();
            r.setAwardId(awardId);
            r.setMemberId(memberId);
            records.add(r);
        }
        int n = recordService.batchInsert(records);
        return null;
    }

    @GetMapping("/delete/member")
    public Object deleteByMemberId(@RequestParam("id")Integer memberId){
        int n = recordService.deleteByMemberId(memberId);
        return null;
    }

    @GetMapping("/delete/award")
    public Object deleteByAwardId(@RequestParam("id") Integer awardId){
        int n = recordService.deleteByAwardId(awardId);
        return null;
    }

    @GetMapping("/delete/setting")
    public Object deleteBySettingId(@SessionAttribute("user") User user){
        int n = recordService.deleteBySettingId(user.getSettingId());
        return null;
    }
}
